﻿using System;
using System.Collections.Generic;
using System.Text;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class StringUtil
    {
        /// <summary>
        /// 判断字符串为空
        /// </summary>
        /// <param name="source">字符串</param>
        /// <returns>true/false</returns>
        public static bool IsNullOrEmptyOrLengthEqualsZero(string source, bool ignoreBlank = true)
        {
            if (ignoreBlank == false)
            {
                return (string.IsNullOrEmpty(source) == true || source.Length == 0);
            }
            else
            {
                return (string.IsNullOrEmpty(source) == true || source.Trim().Length == 0);
            }
        }

        /// <summary>
        /// 判断字符串是否为空
        /// </summary>
        /// <param name="source">源数据</param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(string source)
        {
            return string.IsNullOrEmpty(source);
        }

        /// <summary>
        /// 字符串比较
        /// </summary>
        /// <param name="source">源数据</param>
        /// <param name="target">目标数据</param>
        /// <param name="ignoreCase">是否区分大小写</param>
        /// <returns>比较结果</returns>
        public static bool Equals(string source, string target, bool ignoreCase = true)
        {
            bool flag = false;
            if (ignoreCase == true)
            {
                flag = string.Equals(source, target, StringComparison.InvariantCultureIgnoreCase);
            }
            else
            {
                flag = string.Equals(source, target);
            }
            return flag;
        }

        public static string ReplaceString(string source)
        {
            if (!string.IsNullOrEmpty(source))
            {
                return source.Replace("'", "''");
            }
            return source;
        }

        /// <summary>
        /// 截取字符串后三位
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string SplitLastThree(string source)
        {
            if (string.IsNullOrEmpty(source) == true)
            {
                return "";
            }
            if (source.Length < 3)
            {
                return source;
            }
            return source.Substring(source.Length - 3, 3);
        }
    }
}
